package yanc.app

import grails.transaction.Transactional
import org.springframework.beans.factory.annotation.Value

@Transactional
class PhoneService {

    @Value('${phoneService.ul}')
    protected String URLS

    def add(String phoneComm, String imgUrl) {
        def phone = new Phone()
        phone.imgUrl = URLS + imgUrl
        phone.phoneComm = phoneComm
        phone.createDate = new Date()
        phone.save()
        if (!phone.save()) {
            phone.errors.allErrors.each {
                println it
            }
        }
    }

    def update(Phone phone, String imgUrl) {
        if (phone.imgUrl != imgUrl) {
            phone.imgUrl = URLS + imgUrl
            phone.save()
            if (!phone.save()) {
                phone.errors.allErrors.each {
                    println it
                }
            }
        }
    }

    def del(Integer id) {
        def phone = Phone.findById(id)
        phone.delete()
    }
}
